package tommysmapeditor.helper.importer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import tommysmapeditor.input.MapEditorInput;



//TODO refactor existing to work with that
public class TommyFormatImporter implements ImporterFactory {

	public MapEditorInput translate(String pathAndFileName) throws NumberFormatException, IOException {
		File mapFile = new File(pathAndFileName);
		String parentDir = mapFile.getParent();
		System.out.println("parent dir:"+ parentDir);

		
		
		
			FileReader fr = new FileReader(pathAndFileName);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			String readTileWidth =br.readLine();
			System.out.println("tilewidth: "+readTileWidth);
			String readTileHeight =br.readLine();
			System.out.println("tileheight: "+readTileHeight);
			
			int tileWidth= new Integer(readTileWidth);
			int tileHeight=new Integer(readTileHeight);

			
			MapEditorInput ret = 
				new MapEditorInput(
						8,  
						new Integer(tileWidth),  
						new Integer(tileHeight)
				);

			
			int lineCount = 0;
			while( (line=br.readLine())!=null ){
				System.out.println("line:"+line);
				String[] tiles = line.split(";");
				
				if(tiles.length==tileWidth){
					for(int k=0;k<tileWidth;k++){
						ret.tileGrid[k][lineCount]=Integer.valueOf(tiles[k]);
					}
				}else{
					System.out.println("error: width not good in file");
				}
				lineCount++;
			}
			
			
			//in this format, tiles are in a separate file 
			File tiles = new File(parentDir+"\\"+"tileDefs");
			System.out.println("tiles file exist:"+tiles.exists());
			
			FileReader tilesFr = new FileReader(parentDir+"\\"+"tileDefs");
			BufferedReader tilesBr = new BufferedReader(tilesFr);
			
			
			ret.initTilesArray(Integer.valueOf(tilesBr.readLine()));
			
			ret.EDITOR_WORK_TILES_PATH = parentDir;
			
			
			ret.tileFilePaths = new String[ret._tileNumber];
			for(int i=0;i<ret._tileNumber;i++){
				
				String tileFileName= tilesBr.readLine();
				
				
				Image tile = new Image(Display.getCurrent(),ret.EDITOR_WORK_TILES_PATH+"/"+tileFileName);
				
				ret.tileFilePaths[i]= tileFileName;
				
				
				final int dblwidth = tile.getBounds().width; 
				final int dblheight = tile.getBounds().height; 

				
				Image tileX2 =
					new Image(Display.getDefault(), 
							tile.getImageData()
								.scaledTo((int)(dblwidth * 2),(int)(dblheight * 2)));
				Image tileX3 = 
					new Image(Display.getDefault(), 
							tile.getImageData()
								.scaledTo((int)(dblwidth * 3),(int)(dblheight * 3)));
				ret.toDisposeOnSHutdown.add(tile);
				ret.toDisposeOnSHutdown.add(tileX2);
				ret.toDisposeOnSHutdown.add(tileX3);
				
				ret.tiles[0][i]=tile;
				ret.tiles[1][i]=tileX2;
				ret.tiles[2][i]=tileX3;
				
			}

			
		return ret;
	}

}
